package com.dev.service.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dev.common.constant.Constant;
import com.dev.common.excel.ExcelUtil;
import com.dev.common.mail.MailHelper;
import com.dev.common.util.DateUtil;
import com.dev.common.util.MD5;
import com.dev.common.util.RegexUtil;
import com.dev.facade.entity.SyUserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import com.dev.facade.service.SyUserInfoService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @Description
 * @author Shiwen .Li
 * @date 2016年7月14日 下午3:30:50
 * @version V1.3.1
 */
@Component
public class SyUserInfoServiceImpl extends  BaseServiceImpl  implements
		SyUserInfoService {

	@Override
	public ArrayList<SyUserInfo> selectList(SyUserInfo user) {
		logger.info("查询用户信息{}",user);
		return (ArrayList<SyUserInfo>) userInfoMapper.getList(user);
	}

	@Override
	public int addUser(SyUserInfo user) {
		logger.info("添加用户信息{}",user);
		// md5加密码
		if (!StringUtils.isEmpty(user.getPassword())) {
			user.setPassword(MD5.encryption(user.getPassword()));
		} else {
			logger.info("设置默认密码");
			user.setPassword(Constant.initPassword);
		}

		// 是否手机号注册
		if (RegexUtil.isTrue(user.getAccount(), RegexUtil.MOBILE)) {
			user.setPhone(user.getAccount());
			//TODO 发送短信
		}

		// 是否邮件注册
		if (RegexUtil.isTrue(user.getAccount(), RegexUtil.EMAIL)) {
			user.setEmail(user.getAccount());
			logger.info("发送激活邮件！！");
			mailHelper.sendHtmlMail(user.getAccount(),"账号激活","\"亲爱的\"."+user.getAccount()+".\"：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> \n" +
					"    <a href='http://www.51nworld.cn/user/active?account="+user.getAccount()+"" +
					"'_blank'>http://www.51nworld.cn/user/active?account="+user.getAccount()+"</a><br/> \n" +
					"  如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。\";");
		}
		user.setStatus(0);//未激活状态
		user.setOpreateTime(new Date());
		return userInfoMapper.insertSelective(user);
	}

	@Override
	public int update(SyUserInfo userinfo) {
		userinfo.setOpreateTime(new Date());
		return userInfoMapper.updateByPrimaryKeySelective(userinfo);
	}

	@Override
	public void exportExcel(HttpServletResponse response) throws IOException {
		//
		HSSFWorkbook wb = new HSSFWorkbook();

		SyUserInfo user = new SyUserInfo();
		List<SyUserInfo> userList = userInfoMapper.getList(user);
		logger.info("用户列表信息{}",userList);
		// 表头
		String[] excelHeader = { "账号", "昵称", "邮箱", "手机", "身份证号", "操作时间" };

		HSSFSheet sheet = wb.createSheet("用户信息");
		
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		for (int i = 0; i < excelHeader.length; i++) {
			//设置自适应宽度
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(excelHeader[i]);
			cell.setCellStyle(style);
			sheet.autoSizeColumn(i); 
			sheet.autoSizeColumn(i, true);
		}
		for (int i = 0, size = userList.size(); i < size; i++) {
			row = sheet.createRow(i + 1);
			user = userList.get(i);
			row.createCell(0).setCellValue(user.getAccount());
			row.createCell(1).setCellValue(user.getNickName());
			row.createCell(2).setCellValue(user.getEmail());
			row.createCell(3).setCellValue(user.getPhone());
			row.createCell(4).setCellValue(user.getIdCard());
			row.createCell(5).setCellValue(DateUtil.format2str(user.getOpreateTime(), "yyyy-mm-dd HH:MM:ss"));
		}

		ExcelUtil.export(wb, response);

	}


	@Override
	public Page<SyUserInfo> getListByPage(SyUserInfo user) {
		PageHelper.startPage(user.getNumber(), user.getLimit());
		return userInfoMapper.getListByPage(user);
	}

}
